<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style>
        .index-outside > section .section-main {
            min-width: 0px !important;
            padding: 15px;
        }

        .index-outside > section {
            padding-bottom: 0px !important;
        }
    </style>
</head>
<body>
<!--头部-->
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/mall/spec/save.do">
                    <input type="hidden" name="id" value="${mallSpec.id}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">规格名称<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" lay-verify="name" autocomplete="off" class="layui-input"
                                       value="${mallSpec.name}">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>

<script>
    function ajaxSubmit(data, url, cb) {
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: data,
            dataType: 'json',
            success: function (result) {
                typeof cb === 'function' && cb(result);
            },
            url: url
        });
    }

    function verify() {
        var flag = true;
        var specName = $("[name=name]").val();
        if (!util.isValid(specName)) {
            util.layerMsgError("规格名不能为空")
            return false;
        } else {
            function cb(res) {
                flag = res.success;
                if (!res.success) {
                    util.layerMsgError(res.description)
                    return false;
                }
            }

            ajaxSubmit({
                name: specName,
                id: '${mallSpec.id}'
            }, '${ctx}/mall/spec/verifySpecName.do', cb);
        }
        return flag;
    }

    function doSubmit() {
        var flag = false;
        if (verify()) {
            ajaxSubmit($('#myForm').serialize(), '${ctx}/mall/spec/save.do', function (res) {
                flag = res.success;
                if (res.success) {
                    util.layerMsgSuccess("提交成功")
                } else {
                    util.layerMsgError(res.description)
                }
            });
        }
        return flag;
    }
</script>
</body>
</html>